package com.sky.controller.admin;

import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminShopController")
@RequestMapping("/admin/shop")
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 12.设置营业状态接口
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status) {
        log.info("修改店铺状态为：{}",status==1?"营业中":"打烊中");
        //将状态存入redis数据库中
        redisTemplate.opsForValue().set(KEY,status);
        return Result.success();
    }
    //获取营业状态接口
    @GetMapping("/status")
    public Result<Integer> getStatus() {
        // 查询状态（从redis中）
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);

        // 添加空值检查
        if (status == null) {
            status = 0; // 默认状态为打烊中
        }


        log.info("获取店铺状态为：{}", status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }
}
